package ScalaStudy.chapter08

object Test04_MatchObject {
  def main(args: Array[String]): Unit = {


    //////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////   匹配对象  ////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////

    //调用类
    val student = new Student("wq", 18)

    //针对对象实例的内容进行匹配
    val result = student match{
      case Student("wq", 18) => "wq, 18"
      case _ => "else"
    }
    println(result)





  }
}

//定义类
class Student(val name:String, val age: Int)

//定义一个伴生对象
object Student {
  def apply(name: String, age: Int): Unit ={
    new Student(name, age)
  }

  //必须实现一个unapply方法，用来对对象属性进行拆解

  def unapply(student: Student): Option[(String, Int)] = {
    if(student == null){
      None
    } else {
      Some((student.name), student.age)
    }
  }

}